<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        ul {
            list-style: none
        }

        img {
            vertical-align: top
        }

        .box {
            width: 490px;
            height: 170px;
            margin: 100px auto;
            padding: 5px;
            border: 1px solid #ccc;
        }

        .inner {
            width: 490px;
            height: 170px;
            background-color: pink;
            /*overflow: hidden;*/
            position: relative;
        }

        .inner ul {
            width: 1000%;
            position: absolute;
            top: 0;
            left: 0;
        }

        .inner li {
            float: left;
        }

        .square {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        .square span {
            display: inline-block;
            width: 16px;
            height: 16px;
            background-color: #fff;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
        }

        .square span.current {
            background-color: orangered;
            color: #fff;
        }

    </style>
</head>
<body>
<div class="box" id="box">
    <div class="inner">
        <ul>
            <li><a href="#"><img src="images/01.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/02.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/03.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/04.jpg" alt=""/></a></li>
            <li><a href="#"><img src="images/05.jpg" alt=""/></a></li>
        </ul>
        <div class="square">
            <span class="current">1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
        </div>
    </div>
</div>
<script>
    var box = document.getElementById("box");
    var inner = box.children[0];
    var ul = inner.children[0];
    var square = inner.children[1];
    var spans = square.children;
    for (var i = 0; i < spans.length; i++) {
        var span = spans[i];//每一个span
        span.index = i;//给span加一个自定义属性
        span.onmouseenter = function () {
            //排他 干掉所有人
            for (var i = 0; i < spans.length; i++) {
                var span = spans[i];//每一span
                span.className = "";
            }
            // 留下我自己
            this.className = "current";

            var target = -this.index * inner.offsetWidth;
            animate(ul, target, 5);
        }
    }


    function animate(element, target) {
        //先清除定时器
        clearInterval(element.timerId);
        //开启定时器
        element.timerId = setInterval(function () {
            var current = element.offsetLeft;//获取当前位置
            var step = 20;//步长
            step = current < target ? step : -step;
            if (Math.abs(current - target) >= Math.abs(step)) {//如果当前位置小于目标位置 就继续走
                //盒子的距离 = 盒子当前的距离 + 步长
                element.style.left = current + step + "px";
            } else {
                element.style.left = target + "px";//手动放到目标位置
                clearInterval(element.timerId);//清除定时器
            }
        }, 15)
    }
</script>
</body>
</html>